﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FTBS.Models.DAO;
using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;

namespace FTBS.Models.BLO
{
    public class CheckOutBLO
    {
        private CheckOutDAO _checkOutDao = new CheckOutDAO();
        private TicketDAO _ticketDao = new TicketDAO();
        private UserBLO _userBlo = new UserBLO();

        /// <summary>
        /// Generate CodeBooking
        /// </summary>
        /// <author>VuongVT</author>
        /// <returns>CartId</returns>
        public string GenerateCodeBooking()
        {
            return _checkOutDao.GenerateCodeBooking();
        }

        /// <summary>
        /// Booking a Ticket method
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="totalPrice"></param>
        /// <param name="totalTicket"></param>
        /// <param name="note"></param>
        /// <param name="chairId"></param>
        /// <param name="scheduleId"></param>
        /// <returns>0: Don't create booking</returns>
        /// <author>???</author>
        public bool BookingTicket(SearchSchedule searchSchedule, double actualMoneyReceive, string orderCode)
        {
            try
            {
                string[] listTicket = searchSchedule.TicketId.Split(' ');
                Dictionary<int, double> listTicketIdAndPrice = new Dictionary<int, double>();
                for (int i = 0; i < listTicket.Length; i++)
                {
                    var ticket = _ticketDao.GetTicketById(Convert.ToInt32(listTicket[i]));
                    listTicketIdAndPrice.Add(Convert.ToInt32(listTicket[i]), ticket.PurePrice);
                }
                var resultQuery = _checkOutDao.BookingTicket(searchSchedule, listTicketIdAndPrice, actualMoneyReceive, orderCode);
                if (resultQuery == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Insert CartId for People who payment online successful but the choosen tickets are booked by another people
        /// The system also create the CartId but has the Note = ""Hoàn lại tiền" to notify that the staff have to pay back the money for customer.
        /// </summary>
        /// <param name="searchSchedule"></param>
        /// <param name="listTicketIdAndPrice"></param>
        /// <param name="actualMoneyReceive"></param>
        /// <param name="orderCode"></param>
        /// <returns></returns>
        public bool BookingTicketForFailPayment(SearchSchedule searchSchedule, double actualMoneyReceive, string orderCode)
        {
            try
            {
                string[] listTicket = searchSchedule.TicketId.Split(' ');
                Dictionary<int, double> lstTicketIdAndPrice = new Dictionary<int, double>();
                for (int i = 0; i < listTicket.Length; i++)
                {
                    var ticket = _ticketDao.GetTicketById(Convert.ToInt32(listTicket[i]));
                    lstTicketIdAndPrice.Add(Convert.ToInt32(listTicket[i]), ticket.PurePrice);
                }
                var resultQuery = _checkOutDao.BookingTicketForFailPayment(searchSchedule, lstTicketIdAndPrice, actualMoneyReceive, orderCode);
                if (resultQuery == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Check listChair selected is booked or not
        /// </summary>
        /// <param name="listChairId"></param>
        /// <returns>True: Is Booked Ticket -- False: Not Is Booked Ticket</returns>
        public bool IsBookedTicket(string[] strListTicketId)
        {
            List<int> listTicketId = new List<int>();
            for (int i = 0; i < strListTicketId.Length; i++)
            {
                listTicketId.Add(Convert.ToInt32(strListTicketId[i]));
            }
            int resultCheck = _checkOutDao.CheckTicketBooking(listTicketId);
            if (resultCheck == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public double TotalPrice(string[] strListTicketId)
        {
            List<int> listTicketId = new List<int>();
            for (int i = 0; i < strListTicketId.Length; i++)
            {
                listTicketId.Add(Convert.ToInt32(strListTicketId[i]));
            }
            double resultPrice = _checkOutDao.TotalPrice(listTicketId);
            return resultPrice;
        }

        /// <summary>
        /// Send email booking information
        /// </summary>
        /// <param name="searchSchedule"></param>
        /// <param name="orderCode"></param>
        /// <param name="film"></param>
        /// <param name="theater"></param>
        /// <param name="room"></param>
        /// <param name="date"></param>
        /// <param name="time"></param>
        /// <param name="position"></param>
        public void SendEmailPayment(SearchSchedule searchSchedule, string orderCode, string film, string theater, string room, string date, string time)
        {
            string title = "Thanh toán thành công!!!";
            string Str = "<html>";
            Str += "<head>";
            Str += "<title>";
            Str += "</title>";
            Str += "</head>";
            Str += "<body>";
            Str += "<h2>Thông tin đặt vé:</h2>";
            Str += "<table border=1 max-width=50% cellpadding=2px cellspacing=2px>";
            Str += "<tr>";
            Str += "<td>Mã đặt vé</td>";
            Str += "<td>" + orderCode + "</td>";
            Str += "</tr>";
            Str += "<tr>";
            Str += "<td>Họ tên:</td>";
            Str += "<td>" + searchSchedule.FullName + "</td>";
            Str += "</tr>";
            Str += "<tr>";
            Str += "<td>Film: </td>";
            Str += "<td>" + film + "</td>";
            Str += "</tr>";
            Str += "<tr>";
            Str += "<td>Rạp</td>";
            Str += "<td>" + theater + "</td>";
            Str += "</tr>";
            Str += "<tr>";
            Str += "<td>Phòng</td>";
            Str += "<td>" + room + "</td>";
            Str += "</tr>";
            Str += "<tr>";
            Str += "<td>Ngày chiếu: </td>";
            Str += "<td>" + date + "</td>";
            Str += "</tr>";
            Str += "<tr>";
            Str += "<td>Giờ chiếu:</td>";
            Str += "<td>" + time + "</td>";
            Str += "</tr>";
            Str += "<tr>";
            Str += "<td>Ghế:</td>";
            Str += "<td>" + searchSchedule.Position + "</td>";
            Str += "</tr>";
            Str += "</table><br/>";
            Str +=
                "Hãy nhấn vào <a href='http://localhost:4236/'>đây</a> để có thể truy cập vào website.";
            Str += "</body>";
            Str += "</html>";
            _userBlo.SendMail(searchSchedule.Email, searchSchedule.FullName, title, Str);
        }


    }
}